package com.stylefeng.guns.modular.model.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Order;
import com.stylefeng.guns.modular.model.service.IOrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * model控制器
 *
 * @author fengshuonan
 * @Date 2018-08-01 09:46:40
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private String PREFIX = "/model/order/";

    @Autowired
    private IOrderService orderService;


//    /*
//    生成订单表
//     */
//    @RequestMapping("order")
//    public  Object order(HttpServletRequest request, HttpServletResponse response){
//        String  tickettype=request.getParameter("tickettype");  //购票类型即商品名称
//        String  state=request.getParameter("state");  //状态
//        String  price=request.getParameter("price");  //金额
//        String  openid=request.getParameter("openid");  //用户的小程序唯一标识符
//        String  go_site=request.getParameter("go_site");  //上车站点
//        String end_site=request.getParameter("end_site");  //下车站点
//        String weixinnickname=request.getParameter("weixinnickname");  //用户名称
//
//        Date tickettime = new Date();
//        Date newdate=new Date();
//
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        try{
//            tickettime=df.parse((String)request.getParameter("tickettime"));  //购票时间
//            newdate=df.parse((String)request.getParameter("newdate"));  //当前车票有效时间
//
//        }
//        catch(Exception e){
//            e.printStackTrace();
//        }
//
//        return null;
//    }

    /**
     * 跳转到model首页
     */
    @RequestMapping("")
    public String index() {

        return PREFIX + "order.html";
    }

    /**
     * 跳转到添加model
     */
    @RequestMapping("/order_add")
    public String orderAdd() {
        return PREFIX + "order_add.html";
    }

    /**
     * 跳转到修改model
     */
    @RequestMapping("/order_update/{orderId}")
    public String orderUpdate(@PathVariable Integer orderId, Model model) {
        Order order = orderService.selectById(orderId);
        model.addAttribute("item",order);
        LogObjectHolder.me().set(order);
        return PREFIX + "order_edit.html";
    }

    /**
     * 获取model列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return orderService.selectList(null);
    }

    /**
     * 新增model
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Order order) {
        orderService.insert(order);
        return SUCCESS_TIP;
    }

    /**
     * 删除model
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer orderId) {
        orderService.deleteById(orderId);
        return SUCCESS_TIP;
    }

    /**
     * 修改model
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Order order) {
        orderService.updateById(order);
        return SUCCESS_TIP;
    }

    /**
     * model详情
     */
    @RequestMapping(value = "/detail/{orderId}")
    @ResponseBody
    public Object detail(@PathVariable("orderId") Integer orderId) {
        return orderService.selectById(orderId);
    }
}
